var ns, toast, actionsheet, UIAlbumBrowser;
var app = new Vue({
    el: '#app',
    data: {
        id: '',
        imglist: [{url:'file:///E:/APP/zheyou_shopApp/app_zheyoushop/widget/image/sp.jpg'}],
        imgindex: 0,
        PictureType: 2,
        maxuploadcount: CONFIG.maxuploadcount,
        domainURL: CONFIG.url,
        form: {
            ID: 0,
            Name:'',
            BusinessAddress: '',
            BusinessContactPhone: '',
            BusinessHoursStart: '',
            BusinessHoursEnd: '',
            ContactName: '',
            ContactPhone: '',
            Remark: '',
            CoverImage: '',
            BackImg:'',
            YyImg:'',
            WsImg:'',
        },
        select_imglist:[],
        select_imgindex:0,
        currentTime: '12:00',
        showstarttime:false,
        showendtime:false,
        items:0,//图片类型
    },
    methods: {
        get_data: function() {
            var that = this;
            var option = {};
            option.action = "getbusinessinfo";
            ns.post(option, function(succeed, data, err) {
                if (succeed) {
                    console.log(JSON.stringify(data));
                    that.form = data.BusinessInfo;
                }
            });
        },



        choose_picture: function(item) {
            var that = this;
           that.items=item;
            ns.confirmPer('camera', function() {
                that.choose_picture_start();
                
            });
        },
        choose_picture_start: function() {
            var that = this;
            UIAlbumBrowser.imagePicker({
                max: 1,
                styles: {
                    bg: '#000000',
                    mark: {
                        position: 'top_right',
                        size: 20
                    },
                    nav: {
                        bg: '#000000',
                        cancelColor: '#fff',
                        cancelSize: 16,
                        nextStepColor: '#7fff00',
                        nextStepSize: 16
                    },
                    thumbnail: { //（可选项）返回的缩略图配置，**建议本图片不要设置过大** 若已有缩略图，则使用已有的缩略图。若要重新生成缩略图，可先调用清除缓存接口api.clearCache()。
                        w: 100, //（可选项）数字类型；返回的缩略图的宽；默认：原图的宽度
                        h: 100 //（可选项）数字类型；返回的缩略图的宽；默认：原图的高度
                    }
                },
                animation: true,
            }, function(ret) {
                if (ret.eventType == 'nextStep') {
                    if (ret.list && ret.list.length > 0) {
                        that.select_imglist = ret.list;
                        that.select_imgindex = 0;
                        that.UIAlbumBrowser_transPath();
                    }
                    UIAlbumBrowser.closePicker();
                }
                // if (ret.originalPath && ret.originalPath.length > 0) {
                //    that.form.ImagePath=ret.originalPath;
                // }

            });
        },
        UIAlbumBrowser_transPath: function() {
            var that = this;
            var item = that.select_imglist[that.select_imgindex];
            UIAlbumBrowser.transPath({
                path: item.path
            }, function(ret, err) {
                if (ret) {
                    that.select_imgindex++;
                    if (that.select_imgindex < that.select_imglist.length) {
                        that.UIAlbumBrowser_transPath();
                    } else {
                        that.select_imgindex = 0;
                    }
                    if(that.items==1){
                        that.form.CoverImage= ret.path;
                    }
                    if(that.items==2){
                        that.form.BackImg= ret.path;
                    }
                    if(that.items==3){
                        that.form.YyImg= ret.path;
                    }
                    if(that.items==4){
                        that.form.WsImg= ret.path;
                    }
                   
                }
            });     
        },
        remove_img: function (index) {
            var that = this;
            for (var i = 0; i < that.imglist.length; i++) {
                var item = that.imglist[i];
                if (item.index == index) {
                    that.imglist.splice(i, 1);
                }
            }

        },
        do_viewimages: function (activeIndex) {
            var that = this;
            var imageUrls = [];
            for (var i = 0; i < that.imglist.length; i++) {
                imageUrls.push(that.imglist[i].url);
            }
            var imageBrowser = api.require('imageBrowser');
            imageBrowser.openImages({
                showList: false,
                activeIndex: activeIndex,
                imageUrls: imageUrls
            });
        },
        chooseStarttime:function(){
            var that=this;
            that.showstarttime=!that.showtime;
        },
        onconfirmstart(value){
            var that=this;
            console.log(JSON.stringify(value));
            that.showstarttime=false;
            that.form.BusinessHoursStart=value;
        },
        closeconfirm(){
            var that=this;
            that.showstarttime=false;
            that.showendtime=false;
        },
        chooseEndtime:function(){
            var that=this;
            that.showendtime=!that.showendtime;
        },
        onconfirmend(value){
            var that=this;
            that.showendtime=false;
            that.form.BusinessHoursEnd=value;
        },
        do_save:function(){
            var that = this;
            if (!that.form.Name) {
                api.toast({
                    msg: '请输入商家名称',
                    duration: 3000,
                    location: 'bottom'
                });
                return;
            }
            if (!that.form.BusinessAddress) {
                api.toast({
                    msg: '请输入商家地址',
                    duration: 3000,
                    location: 'bottom'
                });
                return;
            }
            if (!that.form.BusinessContactPhone) {
                api.toast({
                    msg: '请输入商家名称',
                    duration: 3000,
                    location: 'bottom'
                });
                return;
            }
            if (!that.form.BusinessHoursStart) {
                api.toast({
                    msg: '营业时间不能为空',
                    duration: 3000,
                    location: 'bottom'
                });
                return;
            }
            if (!that.form.BusinessHoursEnd) {
                api.toast({
                    msg: '营业时间不能为空',
                    duration: 3000,
                    location: 'bottom'
                });
                return;
            }
            if (!that.form.ContactName) {
                api.toast({
                    msg: '商家联系人不能为空',
                    duration: 3000,
                    location: 'bottom'
                });
                return;
            }
            if (!that.form.ContactPhone) {
                api.toast({
                    msg: '商家联系人不能为空',
                    duration: 3000,
                    location: 'bottom'
                });
                return;
            }
            if (!that.form.CoverImage) {
                api.toast({
                    msg: '请上传商家封面图',
                    duration: 3000,
                    location: 'bottom'
                });
                return;
            }
            if (!that.form.BackImg) {
                api.toast({
                    msg: '请上传商家背景图',
                    duration: 3000,
                    location: 'bottom'
                });
                return;
            }
            if (!that.form.YyImg) {
                api.toast({
                    msg: '请上传营业执照',
                    duration: 3000,
                    location: 'bottom'
                });
                return;
            }
            if (!that.form.WsImg) {
                api.toast({
                    msg: '请上传卫生许可证',
                    duration: 3000,
                    location: 'bottom'
                });
                return;
            }
            var options={};
            options.action='savemallbusiness';
            options.ID=that.form.ID;
            options.BusinessName=that.form.Name;
            options.BusinessAddress=that.form.BusinessAddress;
            options.BusinessContactPhone=that.form.BusinessContactPhone;
            options.BusinessHoursStart=that.form.BusinessHoursStart;
            options.BusinessHoursEnd=that.form.BusinessHoursEnd;
            options.ContactName=that.form.ContactName;
            options.ContactPhone=that.form.ContactPhone;
            options.Remark=that.form.Remark;
            options.CoverImg=that.form.CoverImage;
            options.BackImg=that.form.BackImg;
            options.YyImg=that.form.YyImg;
            options.WsImg=that.form.WsImg;
            console.log(JSON.stringify(options));
            ns.post(options, function(succeed, data, err) {
                if(succeed){
                    if(data.status){
                        api.toast({
                            msg: data.text,
                            duration: 2000,
                            location: 'bottom'
                        });
                        api.sendEvent({
                            name: 'edit_shop'
                        });
                        setTimeout(function() {
                            api.closeWin();
                        }, 500);
                       
                    }
                }else{
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }

            });
        }
       

         
    }
});
apiready = function() {
    api.parseTapmode();
    ns = window.Foresight.Util;
    toast = new auiToast();
    actionsheet = new auiActionsheet();
    UIAlbumBrowser = api.require('UIAlbumBrowser');
    bmLocation = api.require('bmLocation');
    app.id = api.pageParam.id;
    app.get_data();

};

     